package cn.com.chec.en.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.chec.en.model.domain.UserInfo;

public class UserLoginFilter implements Filter {
	private static final long serialVersionUID = -1998907343910499L;

	public void init(FilterConfig config) {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;

		UserInfo userInfo = (UserInfo) req.getSession().getAttribute(
				"systemUserInfo");

		String requestURI = req.getRequestURI();
		if ((userInfo == null) && (requestURI.contains("/admin"))) {
			resp.sendRedirect(req.getScheme() + "://" + req.getServerName()
					+ ":" + req.getServerPort() + req.getContextPath()
					+ "/logon");
			return;
		}
		chain.doFilter(request, response);
	}

	public void destroy() {
	}
}